﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using Sinacor.Infra.UI.Controls.SmartClient;
using Sinacor.UI.Bovespa.ContaCorrente.Presenter;
using Sinacor.Infra.UI.Controls.SmartClient.Validation;
using Sinacor.Infra.UI.Common;
using PSE.Framework.UI.Resources;
using Sinacor.Infra.UI.Controls.SmartClient;

namespace Sinacor.UI.Bovespa.ContaCorrente.View.SmartClient
{
    public partial class PesquisaHistoricoContaCorrente : SinacorWindow, IPesquisaHistoricoContaCorrente
    {
        public Presenter.PesquisaHistoricoContaCorrente presenter { get; set; }

        public Model.HistoricoContaCorrente HistoricoContaCorrenteSelecionado { get; set; }

        public PesquisaHistoricoContaCorrente()
        {
            InitializeComponent();

            this.presenter = new Presenter.PesquisaHistoricoContaCorrente(this);

            this.DataContext = this.presenter;

            this.dtgHistorico.NewItemType = typeof(Model.HistoricoContaCorrente);

            this.AfterInitialize += new RoutedEventHandler(PesquisaHistoricoContaCorrente_AfterInitialize);
        }

        private void PesquisaHistoricoContaCorrente_AfterInitialize(object sender, RoutedEventArgs e)
        {
            this.presenter.Inicializar();
            this.presenter.ConsultarHistoricoContasCorrentes();
            this.AtualizarHistoricoContaCorrente();
            this.HabilitarGrid();
        }

        public void AtualizarHistoricoContaCorrente()
        {
            dtgHistorico.UpdateTarget(SinacorDataGrid.ItemsSourceProperty);
        }

        private void HabilitarGrid()
        {
            dtgHistorico.IsEnabled = (presenter.ListaHistoricoContaCorrente.Count > 0);
        }

        private void dtgHistorico_MouseDoubleClick(object sender, MouseButtonEventArgs e)
        {
            HistoricoContaCorrenteSelecionado = this.presenter.HistoricoContaCorrenteSelecionada;
            this.Close();
        }

        private void SinacorButton_Click(object sender, RoutedEventArgs e)
        {
            this.presenter.FiltroHistoricoContaCorrente.Descricao = txtPesquisaDescricao.Text;
            this.presenter.ConsultarHistoricoContasCorrentes();
            this.AtualizarHistoricoContaCorrente();
            this.HabilitarGrid();
        }        

        private void btnNovaPesquisa_Click(object sender, RoutedEventArgs e)
        {
            this.presenter.LimpaFiltro();
            txtPesquisaDescricao.UpdateTarget(SinacorTextBox.TextProperty);

            this.presenter.LimpaListaHistorico();
            AtualizarHistoricoContaCorrente();

            this.HabilitarGrid();
            txtPesquisaDescricao.Focus();

           
        }
    }
}
